# -*- coding: utf-8 -*-
# This code written by ChanGOGOGO
"""
自动大模型质检
"""
import time
from apscheduler.schedulers.blocking import BlockingScheduler
from export_class.export_llm_inspection import ExportLLMInspection
from util import (
    get_today_date,
    get_start_end_time,
    server_ip,
    server_port,
    wecom_push_msg,
    get_time_desc,
    get_nearby_day,
)


current_push_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1668b06d-169a-4f93-9f43-5b19c76dbcb6"  # 生产
# current_push_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=452ad12a-84ee-4631-9f85-dc6f1679c67b'  # 测试


def get_yesterday_month(yesterday: str):
    """获取昨天所在的月份"""
    yesterday_split = yesterday.split("-")
    yesterday_month = yesterday_split[0] + yesterday_split[1]
    return yesterday_month


def quality_inspection(business_infos: list):
    today = get_today_date()
    yesterday = get_nearby_day(today)
    # start_time, end_time = ('2024-10-21 15:20:00', '2024-10-21 15:23:00')
    start_time, end_time = get_start_end_time(yesterday)
    date_month = get_yesterday_month(yesterday)
    for business_info in business_infos:
        business, company_id, business_id = business_info
        req = {
            "target_datas": [],
            "data_date": date_month,
            "company_id": company_id,
            "business_id": business_id,
            "business": business,
            "data_type": "大模型质检",
            "task_type": "活跃任务",
            "start_time": start_time,
            "end_time": end_time,
        }
        export_quality_inspection = ExportLLMInspection(req)
        export_start_time = time.time()
        res = export_quality_inspection.export()
        export_end_time = time.time()
        export_task_time = export_end_time - export_start_time
        export_task_time_desc = get_time_desc(export_task_time)
        code = res.get("code")
        if code == 1:
            ...
        else:
            excel_name = res.get("excel_names")[0]
            url = (
                f"http://{server_ip}:{server_port}/DownloadFile?file_name={excel_name}"
            )
            wecom_msg = (
                f"以下为“{business}”昨天({yesterday})拨打数据的大模型质检结果(保留7天)，"
                f"质检耗时{export_task_time_desc}：\n[{excel_name}]({url})"
            )
            wecom_push_msg(current_push_url, msg=wecom_msg)


if __name__ == "__main__":
    target_business_infos = [
        ("【数曜】教育", "1051", "1001"),
        ("【三云】教育", "1051", "1003"),
        ("【国新】教育", "1051", "1004"),
    ]
    # quality_inspection(target_business_infos)
    scheduler = BlockingScheduler()
    scheduler.add_job(
        quality_inspection,
        args=[target_business_infos],
        trigger="cron",
        hour=0,
        minute=10,
    )
    scheduler.start()
